package com.yisu.expressway.manager.core.service.impl;

import com.yisu.expressway.ewf.common.enums.ResponseCode;
import com.yisu.expressway.ewf.common.exception.StageException;
import com.yisu.expressway.ewf.common.utils.QiNiuUploadUtil;
import com.yisu.expressway.manager.domain.config.SystemConfig;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by xlt on 2017/4/24.
 */
public class BaseService {
    @Resource
    protected SystemConfig systemConfig;

    protected String uploadFile(MultipartFile file) {
        String originalFileName = file.getOriginalFilename();
        String newFileName = QiNiuUploadUtil.generateFileName() + originalFileName.substring(originalFileName.lastIndexOf("."));

        QiNiuUploadUtil.Uploader uploader = new QiNiuUploadUtil.Uploader();
        uploader.setAccessKey(systemConfig.getQiNiuAccessKey());
        uploader.setSecretKey(systemConfig.getQiNiuSecretKey());
        uploader.setBucketName(systemConfig.getQiNiuBucketName());
        uploader.setFileName(newFileName);
        try {
            byte[] data = new byte[(int)file.getSize()];
            InputStream in = file.getInputStream();
            in.read(data);
            uploader.setData(data);
            if(QiNiuUploadUtil.upload(uploader)) {
                StringBuilder sb = new StringBuilder(80);
                sb.append(systemConfig.getQiNiuImageHost());
                if(!systemConfig.getQiNiuImageHost().endsWith("/")) {
                    sb.append("/");
                }
                sb.append(newFileName);
                return sb.toString();
            } else {
                throw new StageException(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
            }
        } catch (Exception e) {
            throw new StageException(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }

    protected String uploadFileForLocation(File file)
    {
        String originalFileName = file.getName();
        String newFileName = QiNiuUploadUtil.generateFileName() + originalFileName.substring(originalFileName.lastIndexOf("."));
        QiNiuUploadUtil.Uploader uploader = new QiNiuUploadUtil.Uploader();
        uploader.setAccessKey(systemConfig.getQiNiuAccessKey());
        uploader.setSecretKey(systemConfig.getQiNiuSecretKey());
        uploader.setBucketName(systemConfig.getQiNiuBucketName());
        uploader.setFileName(newFileName);
        try {
            byte[] data = new byte[(int)file.length()];
            InputStream in = new FileInputStream(file.getAbsolutePath());
            in.read(data);
            uploader.setData(data);
            if(QiNiuUploadUtil.upload(uploader)) {
                StringBuilder sb = new StringBuilder(80);
                sb.append(systemConfig.getQiNiuImageHost());
                if(!systemConfig.getQiNiuImageHost().endsWith("/")) {
                    sb.append("/");
                }
                sb.append(newFileName);
                return sb.toString();
            } else {
                throw new StageException(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
            }
        }
        catch (Exception ex)
        {
            throw new StageException(ResponseCode.FAILURE.code, ResponseCode.FAILURE.msg);
        }
    }
}
